package com.dongway.netcondition.socketToBox;

import java.io.IOException;
import java.io.OutputStream;
import java.net.Socket;

import com.dongway.netcondition.model.DevInfo;

public class GetNetWork {

    public static DevInfo getInfor(Integer dev_id) {

        DevInfo devInfo = new DevInfo();
        devInfo.setId(dev_id);

        // 根据ID获取Socket
        Socket socket = SocketContent.getInstance().getSocket(dev_id);
        // Map<String ,String> map= new HashMap<String, String>();
        // InputStream in=null;
        String hex = "";
        // 发送报文头
        byte[] b1 =
                { 0x61, 0x31, 0x30, 0x2c, (byte) 0x88, (byte) 0x87, 0x01, 0x06, 0x02, 0x00, 0x00,
                 0x00 };

        // 把ID转为16进制存储到byte中
        hex = Integer.toHexString(dev_id);
        hex = hex.length() > 2 ? hex : ("0" + hex);
        byte[] b2 = HexStringToBytes.hexStringToBytes(hex);
        b1[10] = b2[0];

        try {
            OutputStream out = socket.getOutputStream();
            out.write(b1);
            out.flush();
        } catch (IOException e) {
            System.out.println("查询失败>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>");
        }
      
        SocketInfo info = SocketContent.getInfo(dev_id);
        while (info.getNetInfo() == null) {
            System.out.println("网络信息查询中。。。。。。");
            // 为空，一直等！
            try {
				Thread.sleep(1000);
			} catch (InterruptedException ige) {
			}
        }
        // 根据ID获取字节信息
        byte[] b = info.getNetInfo();
        String str = "";
        int len = b.length;
        //
        // out.write(b1);
        // out.flush();
        // // out.close();
        //
        //
        //
        //
        // //接收信息
        // in= socket.getInputStream();
        // byte[] b = new byte[1024];
        // String str="";
        // int len = in.read(b);
        //
        // //对接收的信息进行预处理
        // while(len!=156)
        // { out.write(b1);
        // out.flush();
        // // 刷新输入
        // in = socket.getInputStream();
        // b = new byte[1024];
        // len = in.read(b);
        //
        // }
        //
        // str= new String(b,0,len);

        int[] inf = new int[len];
        for (int i = 0; i < len; i++) {

            inf[i] = (int) b[i];
            if (inf[i] < 0)
                inf[i] += 256;

            // 网卡B的IPַ
            if (15 < i && i < 32) {
                if (i == 16)
                    str = "";
                if (inf[i] != 46 && 48 <= inf[i]) {
                    str += (inf[i] - 48) + "";
                } else if (inf[i] == 46) {
                    str += ".";
                }
                if (i == 31) {
                    devInfo.setNetCardBIp(str);
                }
            }

            // 网卡B的子网掩码
            if (31 < i && i < 48) {
                if (i == 32) {
                    str = "";
                }
                if (inf[i] != 46 && 48 <= inf[i]) {
                    str += (inf[i] - 48) + "";
                } else if (inf[i] == 46) {
                    str += ".";
                }
                if (i == 47) {
                    devInfo.setNetCardBSubIp(str);
                }
            }
            // 网卡B的默认网关
            if (i == 51) {
                str = "";
                str = inf[48] + "." + inf[49] + "." + inf[50] + "." + inf[51];
                devInfo.setNetCardBGatewayIp(str);
            }
            // 网卡B的Mac 地址
            if (51 < i && i < 58) {
                if (i == 52) {
                    str = "";
                }
                hex = Integer.toHexString(inf[i]);
                if (i == 57) {
                    str += (hex.length() == 2 ? hex : ("0" + hex));
                    devInfo.setNetCardBMacAddress(str);
                } else
                    str += (hex.length() == 2 ? hex : ("0" + hex)) + ":";

            }

            // 网卡A的IPַ
            if (63 < i && i < 80) {
                if (i == 64) {
                    str = "";
                }
                if (inf[i] != 46 && 48 <= inf[i]) {
                    str += (inf[i] - 48) + "";
                } else if (inf[i] == 46) {
                    str += ".";
                }

                if (i == 79) {
                    devInfo.setNetCardAIp(str);
                }
            }

            // 网卡A的子网掩码
            if (79 < i && i < 96) {
                if (i == 80) {
                    str = "";
                }
                if (inf[i] != 46 && 48 <= inf[i]) {
                    str += (inf[i] - 48) + "";
                } else if (inf[i] == 46) {
                    str += ".";
                }

                if (i == 95) {
                    devInfo.setNetCardASubIp(str);
                }
            }
            // 网卡A的默认网关
            if (i == 99) {
                str = "";
                str = inf[96] + "." + inf[97] + "." + inf[98] + "." + inf[99];
                devInfo.setNetCardAGatewayIp(str);
            }
            // 网卡A的Mac地址
            if (99 < i && i < 106) {
                if (i == 100) {
                    str = "";
                }
                hex = Integer.toHexString(inf[i]);
                if (i == 105) {
                    str += (hex.length() == 2 ? hex : ("0" + hex));
                    devInfo.setNetCardAMacAddress(str);

                } else
                    str += (hex.length() == 2 ? hex : ("0" + hex)) + ":";

            }

            // 3G网卡ip
            if (111 < i && i < 127) {
                if (i == 112) {
                    str = "";
                }
                if (inf[i] != 46 && 48 <= inf[i]) {
                    str += (inf[i] - 48) + "";
                } else if (inf[i] == 46) {
                    str += ".";
                }

                if (i == 126) {
                    devInfo.setNetCard3gIp(str);
                }
            }

            // 3G子网掩码
            if (127 < i && i < 143) {
                if (i == 128) {
                    str = "";
                }
                if (inf[i] != 46 && 48 <= inf[i]) {
                    str += (inf[i] - 48) + "";
                } else if (inf[i] == 46) {
                    str += ".";
                }
                if (i == 142) {
                    devInfo.setNetCard3gSubIp(str);
                }
            }
            // 3G默认网关
            if (i == 147) {
                str = "";
                str = inf[144] + "." + inf[145] + "." + inf[146] + "." + inf[147];
                devInfo.setNetCard3gGatewayIp(str);
            }
            // 3G的Mac地址
            if (147 < i && i < 154) {
                if (i == 148) {
                    str = "";
                }
                hex = Integer.toHexString(inf[i]);
                if (i == 153) {
                    str += (hex.length() == 2 ? hex : ("0" + hex));
                    devInfo.setNetCard3gMacAddress(str);
                } else
                    str += (hex.length() == 2 ? hex : ("0" + hex)) + ":";

            }

        }
         System.out.println(devInfo);
        return devInfo;
    }

}
